<?php

class Con_up extends Acan{
	
	public $defaultAct='index';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
	}
	function Act_index(){
		include $this->Atpl();
	}
	//--同步文件
	function Act_sync(){
		$type = $this->id;
		$anmp = Afun_new('anmp');
		$lists = $anmp->find_list('acan').$anmp->find_list($type);
		$data = $anmp->md5_list($lists,'','sync_'.$type);
		$anmp->up_dev($data);
		$msg = '生成'.$type.'文件缓存完毕<br>';
		$msg .= '生成更新文件成功<br>';
		$msg .= '<a href="'.$this->Amodr.'-sync_2-'.$type.'">点击生成dev缓存</a>';
		echo $msg;
	}
	function Act_sync_2(){
		$type = $this->id;
		$anmp = Afun_new('anmp');
		$path_dev = Acan_anmp_sync_path.'/';
		$lists_dev = $anmp->up_list('',$path_dev);
		$data = $anmp->md5_list($lists_dev,$path_dev,'dev_'.$type);
		$msg = '生成'.$type.'对比缓存完毕<br>';
		$msg .= '<a href="'.$this->Amodr.'-sync_3-'.$type.'">点击对比</a>';
		echo $msg;
	}
	function Act_sync_3(){
		$type = $this->id;
		$Aobj_new = Afun_filecache_get('sync_'.$type,'./adata/up/');
		$Aobj = Afun_filecache_get('dev_'.$type,'./adata/up/');
		foreach ($Aobj_new->files as $key => $val) {
			if(empty($Aobj->files[$key])){
				$Achg[] = $key;//新加
				$msg_up .= $key.'<br>';
			}elseif($Aobj->files[$key]!=$val){
				$Achg[] = $key;//修改
				$msg_up .= $key.'<br>';
			}
		}
		$Adata->files = $Achg;
		$Adata->num = count($Achg);
		Afun_filecache_set('sync_up_'.$type,$Adata);
		if($Adata->num == 0){echo '需要同步0个文件';}else{
			$msg .= '总共需要同步'.$Adata->num.'个文件';
			$msg .= '<hr>';
			$msg .= '对比完成<a href="'.$this->Amodr.'-sync_4-'.$type.'">点击同步</a><hr>';
			echo $msg.$msg_up;
		}
	}
	function Act_sync_4(){
		$type = $this->id;
		$path = str_replace('\\','/',Acan_anmp_path).'/';
		$Adata = Afun_filecache_get('sync_up_'.$type);
		if(empty($Adata->files)){Afun_refresh('同步完毕',$this->Amodr.'-sync');}
		foreach($Adata->files as $k=>$v){
			$dev_file = Acan_anmp_sync_path.'/'.$v;
			if(!file_exists(dirname($dev_file))){Afun_file_mkdirm(dirname($dev_file));}
			if(file_exists($dev_file)){unlink($dev_file);}
			copy($path.$v,$dev_file);
		//	unset($Adata->files[$k]);
		//	$Adata->have = count($Adata->files);
		//	Afun_filecache_set('sync_'.$type,$Adata);
		//	echo '已同步'.($Adata->num - $Adata->have).'个文件,还剩'.$Adata->have.'个文件';
		//	Afun_refresh('',$this->Amodr.'-sync_copy');
		}
		Afun_refresh('同步完毕',$this->Amodr.'-sync-'.$type);
	}
	function Act_update(){
		$type = $this->id;
		$anmp = Afun_new('anmp');
		$lists = $anmp->find_list('acan').$anmp->find_list($type);
		$anmp->md5_list($lists,'','update_'.$type);
		$Aobj_new = unserialize(file_get_contents(Acan_anmp_update_url));
		Afun_filecache_set('update_new_'.$type,$Aobj_new,0,'./adata/up/');
		$Aobj = Afun_filecache_get('update_'.$type,'./adata/up/');
		if($Aobj_new->version_c > $Aobj->version_c){
			$msg = '控制台发现新版本 v'.$Aobj_new->version_c.' <a href="'.$this->Amodr.'-update_2-'.$type.'">点击更新</a>';
		}else{
			$msg = '你目前使用是最新版本,无需更新 <a href="'.$this->Amodr.'-update_2-'.$type.'">点击检测文件</a>';
		}
		echo $msg;
	}
	function Act_update_2(){
		$type = $this->id;
		$Aobj_new = Afun_filecache_get('update_new_'.$type,'./adata/up/');
		$Aobj = Afun_filecache_get('update_'.$type,'./adata/up/');
		foreach ($Aobj_new->files as $key => $val) {
			if(empty($Aobj->files[$key])){
				$Achg[] = $key;//新加
				$msg_up .= $key.'<br>';
			}elseif($Aobj->files[$key]!=$val){
				$Achg[] = $key;//修改
				$msg_up .= $key.'<br>';
			}
		}
		$Adata->files = $Achg;
		$Adata->num = count($Achg);
		if($Adata->num == 0){echo '需要更新0个文件';}else{
			echo '总共需要更新'.$Adata->num.'个文件';
			echo '<hr>';
			echo '对比完成 <a href="'.$this->Amodr.'-update_3-'.$type.'">开始更新</a>';
			echo '<hr>';
			echo $msg_up;
		}
		Afun_filecache_set('update_chg_'.$type,$Adata);
	}
	function Act_update_3(){
		$type = $this->id;
		$upurl = Acan_anmp_update_urlpath;
		$path = A_ROOT.'./temp/update/';
		$Adata = Afun_filecache_get('update_chg_'.$type);
		if(empty($Adata->files)){Afun_refresh('',$this->Amodr.'-update-'.$type);}
		foreach($Adata->files as $k=>$v){
			if(!file_exists(dirname($path.$v))){Afun_file_mkdirm(dirname($path.$v));}
			if(file_exists($path.$v)){unlink($path.$v);}
			file_put_contents($path.$v,file_get_contents($upurl.$v));
			unset($Adata->files[$k]);
			$Adata->have = count($Adata->files);
			Afun_filecache_set('update_chg_'.$type,$Adata);
			echo '已下载'.$Adata->num - $Adata->have.'个文件,还剩'.$Adata->have.'个文件';
			Afun_refresh('',$this->Amodr.'-update_4-'.$type);
		}
	}
	function Act_update_4(){
		$type = $this->id;
		$prr = explode(':',Acan_anmp_path);
		$cmd = $prr[0].': && xcopy '.A_ROOT.'temp\update '.Acan_anmp_path.'\ /E /Y';
		echo Afun_run_cmd($cmd);
		Afun_file_rmdirm(A_ROOT.'temp\update\\'); 
		Afun_refresh('升级成功',$this->Amodr.'-index');
	}
}

?>